package com.wangzeqi.changliaoba.contacts;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.easemob.chat.EMChatManager;
import com.easemob.exceptions.EaseMobException;
import com.wangzeqi.changliaba.dao.AddPersonEntity;
import com.wangzeqi.changliaoba.R;
import com.wangzeqi.changliaoba.singleton.AllSingleTon;

import java.util.List;

import de.greenrobot.dao.query.QueryBuilder;

/**
 * Created by dllo on 16/3/4.
 */
public class AddPersonAdapter extends BaseAdapter {

    private Context context;
    List<AddPersonEntity> datas;


    public AddPersonAdapter(Context context) {
        this.context = context;
    }

    public void addData(List<AddPersonEntity> data) {
        this.datas = data;
       notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        if (datas!= null){
            return datas.size();
        }else {
            return 0;
        }
    }

    @Override
    public Object getItem(int position) {

        if (datas!= null && datas.size()>0){
            return datas.get(position);
        }else {
            return 0;
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_add_person_list_layout, null);
            holder.textViewName = (TextView) convertView.findViewById(R.id.find_person_tv);
            holder.textViewReason = (TextView) convertView.findViewById(R.id.find_person_tv_reason);
            holder.textViewAgree = (TextView) convertView.findViewById(R.id.add_person_tv_agree);
            holder.textViewOk = (TextView) convertView.findViewById(R.id.add_person_tv_ok);
            holder.textViewWait = (TextView) convertView.findViewById(R.id.add_person_tv_wait);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (datas != null && datas.size() > 0) {
            holder.textViewName.setText(datas.get(position).getName());
            final int type = datas.get(position).getType();
            if (type==1) {
                holder.textViewAgree.setVisibility(View.INVISIBLE);
                holder.textViewOk.setVisibility(View.INVISIBLE);
                holder.textViewWait.setVisibility(View.VISIBLE);
            } else if (type==2) {
                holder.textViewAgree.setVisibility(View.INVISIBLE);
                holder.textViewOk.setVisibility(View.VISIBLE);
                holder.textViewWait.setVisibility(View.INVISIBLE);

            } else {
                holder.textViewAgree.setVisibility(View.VISIBLE);
                holder.textViewOk.setVisibility(View.INVISIBLE);
                holder.textViewWait.setVisibility(View.INVISIBLE);
                holder.textViewReason.setText(datas.get(position).getReason());
                final ViewHolder finalHolder = holder;
                holder.textViewAgree.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        finalHolder.textViewAgree.setVisibility(View.INVISIBLE);
                        finalHolder.textViewOk.setVisibility(View.VISIBLE);
                        try {
                            EMChatManager.getInstance().acceptInvitation(datas.get(position).getName());
                        } catch (EaseMobException e) {
                            e.printStackTrace();
                        }
                        QueryBuilder qb =  AllSingleTon.getOurInstance().getDaoSession().getAddPersonEntityDao().queryBuilder();
                        String name = datas.get(position).getName();
                        qb.list().remove(name);
                        AddPersonEntity data = new AddPersonEntity(EMChatManager.getInstance().getCurrentUser(),name,2,"");
                        AllSingleTon.getOurInstance().getDaoSession().getAddPersonEntityDao().insertOrReplace(data);
                        // 发送一条广播 通知联系人的adapter更新
                        Intent intent = new Intent("com.wangzeqi.changliaoba.contacts.NOTIFY");
                        context.sendBroadcast(intent);
                    }
                });
            }
        }
        return convertView;
    }

    final static class ViewHolder {
        TextView textViewName,textViewReason, textViewAgree, textViewOk, textViewWait;
    }
}
